package com.manelrodriguez.applications.eurostoxx50.activity;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;

import com.manelrodriguez.applications.eurostoxx50.R;
import com.manelrodriguez.applications.eurostoxx50.adapters.CotizacionListAdapter;
import com.manelrodriguez.applications.eurostoxx50.to.Empresa;
import com.manelrodriguez.applications.eurostoxx50.utils.Constantes;

public class FavoritosActivity extends Activity implements OnItemClickListener{
	public static ArrayList<Empresa> favoritos = null;
	private CotizacionListAdapter m_adapter;
	SharedPreferences settings = null;
	
	protected void onCreate(Bundle savedInstanceState) {
		 super.onCreate(savedInstanceState);
	     setContentView(R.layout.favoritos);
	     if(Eurostoxx50Activity.m_empresas !=null && !Eurostoxx50Activity.m_empresas.isEmpty()){
		     favoritos = new ArrayList<Empresa>();
		     settings = getSharedPreferences(Empresa.PREFS_NAME, MODE_PRIVATE);
		     String idFavoritos = settings.getString(Empresa.FAVORITES_IDS, "");
		     if(idFavoritos!= null && !idFavoritos.equals("")){
		    	 String[] ids = idFavoritos.split("#");
		    	 for(int i = 0;i<ids.length;i++){
		    		 favoritos.add(Eurostoxx50Activity.m_empresas.get(Integer.valueOf(ids[i])));
		    	 }
		     }
		     
		     m_adapter = new CotizacionListAdapter(this, R.layout.list_item, favoritos);
		     ListView lv = (ListView)findViewById(R.id.listView2);
		     lv.setAdapter(this.m_adapter);
		     lv.setTextFilterEnabled(true);
		     lv.setOnItemClickListener(this);
		     registerForContextMenu(lv);
	     }
	}

	public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
		Intent intent = new Intent(getApplicationContext(), DetalleCotizacionActivity.class);
    	intent.putExtra(Constantes.EMPRESA, favoritos.get(position));
		startActivity(intent);
	}
	
	@Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
    	if (v.getId()==R.id.listView2) {
    		AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)menuInfo;
    	    menu.setHeaderTitle(favoritos.get(info.position).getCompanyName());
    	    menu.add(0, v.getId(), 0, getString(R.string.borrarFavorito));
    	}
    }
	
	@Override
    public boolean onContextItemSelected(MenuItem item) {
    	AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
    	if (item.getTitle().equals(getString(R.string.borrarFavorito))) {
    		Empresa empresa = favoritos.get(info.position);
    		empresa.borrarFavorito(info.position,this);
    		m_adapter.remove(m_adapter.getItem(info.position));
            Toast.makeText(FavoritosActivity.this, getString(R.string.borrado), Toast.LENGTH_SHORT).show();
            m_adapter.notifyDataSetChanged();
    	}

       return true;
    }
	
}
